package com.chenwang.measure.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseActivity;
import com.chenwang.measure.model.Constant;
import com.chenwang.measure.model.bean.CustomBean;
import com.chenwang.measure.model.bean.LocationBean;
import com.chenwang.measure.model.bean.LonLatBean;
import com.chenwang.measure.ui.dialog.MeasureLoadingDialog;
import com.chenwang.measure.ui.view.TitleLayout;
import com.chenwang.measure.utils.DistanceUtils;
import com.chenwang.measure.utils.MyCrashHandler;
import com.chenwang.measure.utils.MyLogger;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.RxTime;
import com.chenwang.measure.utils.SharePreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//定制测量
public class CustomActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleLayout mTitleLayout;//标题
    @BindView(R.id.iv_location)
    ImageView mIvLocation;  //定位图标
    @BindView(R.id.tv_measure)
    TextView mTvMeasure;    //测量结果 距离
    @BindView(R.id.tv_lat)
    TextView mTvLat;   //纬度
    @BindView(R.id.tv_lon)
    TextView mTvLon;   //经度
    @BindView(R.id.tv_height)
    TextView mTvHeight;  //高度
    @BindView(R.id.tv_satellite_total)
    TextView mTvSatelliteTotal;  // 卫星总数
    @BindView(R.id.tv_satellite)
    TextView mTvSatellite;  //解算卫星
    @BindView(R.id.tv_teleseme)
    TextView mTvtelseme;   //显示当前要测量的信号机
    @BindView(R.id.tv_location_status)
    TextView mTvLocationStatus;//定位状态 0未解算 16单点定位 17差分定位 50固定解
    @BindView(R.id.rb_statr)
    RadioButton mRbStatr;//    开始点
    @BindView(R.id.rb_end)
    RadioButton mRbEnd;//  结束点
    @BindView(R.id.rb_1)
    RadioButton mRb1;
    @BindView(R.id.rb_2)
    RadioButton mRb2;
    private int mNumber;//定位次数
    private int mLine = 1;//1默直 3折
    private RxTime mRxTime;  //定时器
    private LonLatBean mOffsetBean = new LonLatBean(); // 保存折线上一次的点
    private List<LonLatBean> mPoints = new ArrayList<>();//
    private int mTeleseme;//0 默认开始点  1结束点
    private String mMultipoint = ""; //连续点
    private MeasureLoadingDialog mDialog;
    private Double mLatStart;//起始点纬度
    private Double mLonStart;//起始点经度
    private Double mLatEnd;//结束点纬度
    private Double mLonEnd;//结束点经度
    private CustomBean mCustomBean; //测量数据类
    private double mDistance = 0.0;  //距离
    @BindView(R.id.tv_status)
    TextView mTvStatus;
    private CustomBean mAgain;
    private int mIsFixed;
    private double mHorizontal;  //水平精度


    @Override
    public int setLayoutId() {
        return R.layout.activity_custom;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initMeasureNumber();//获取 测量测试
        mIsFixed = SharePreferenceUtils.getInt(this, "fixed", 0);
        mHorizontal = Double.valueOf(SharePreferenceUtils.getString(this, Constant.HORIZONTAL, "0.5"));

    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mRbStatr.setClickable(false);
        mRbEnd.setClickable(true);
        initLocation(); //获取经纬度信息
        mTitleLayout.setClickListenertLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialogBack();//返回 提示
            }
        });
        if (getIntent().getParcelableExtra("data") != null) {
            //获取传过来的数据类
            mCustomBean = getIntent().getParcelableExtra("data");
            mAgain = mCustomBean;
            mRbStatr.setText(mCustomBean.getStartName());
            mRbEnd.setText(mCustomBean.getEndName());
        }
    }

    /**
     * rb 监听
     * @param view 点击的rb
     */
    public void onLineType(View view) {
        switch (view.getId()) {
            case R.id.rb_1://直线
                if (mLine == 3)
                    showLine1();
                break;
            case R.id.rb_2://折线
                if (mLine == 1)
                    showLine3();
                break;

        }
    }

    //折线
    private void showLine3() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("折线");
        normalDialog.setMessage("是否切换折线");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mLine = 3;
                        MyToast.show("折线");
                        mTvtelseme.setVisibility(View.VISIBLE); // 折线p 点

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mRb1.setChecked(true);
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

     //直线
    private void showLine1() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("直线");
        normalDialog.setMessage("是否切换直线");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mLine = 1;
                        MyToast.show("直线");
                        mTvtelseme.setVisibility(View.INVISIBLE);// 折线p 点

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mRb2.setChecked(true);
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }


    private int line1Count;//直线
    private int line3Count;//折线

    //开始点
    private void showStart() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("开始点");
        normalDialog.setMessage("是否切换开始点");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mTeleseme = 0;//0 开始信号机  1结束点信号机  标识
                        MyToast.show("开始点");
                        if (mLine == 3) { //如果为折线 并且line3Count =2  代表 测量折线结束
                            if (line3Count == 1) {
                                line3Count = 2;
                            }
                        }
                        mRbStatr.setClickable(false);
                        mRbEnd.setClickable(true);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mRbEnd.setChecked(true);
                        mRbStatr.setClickable(true);
                        mRbEnd.setClickable(false);
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

    //结束点
    private void showEnd() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("结束点");
        normalDialog.setMessage("是否切换结束点");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mTeleseme = 1; //0 开始信号机  1结束点信号机  标识
                        MyToast.show("结束点");
                        if (mLine == 3) {//如果为折线 并且line3Count =2  代表 测量折线结束
                            if (line3Count == 1) {
                                line3Count = 2;
                            }
                        }
                        mRbStatr.setClickable(true);
                        mRbEnd.setClickable(false);

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        mRbStatr.setChecked(true);
                        mRbStatr.setClickable(false);
                        mRbEnd.setClickable(true);
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }


    /**
     * 信号机开始结束监听事件
     * @param view
     */
    public void onStatrEnd(View view) {
        switch (view.getId()) {
            case R.id.rb_statr://开始点
                showStart();
                break;
            case R.id.rb_end://结束点
                showEnd();
                break;
        }
    }

    //更新经纬度等信息
    private void initLocation() {
        mRxTime = new RxTime();
        mRxTime.interval(1000, new RxTime.RxAction() {
            @Override
            public void action(long number) {
                if (LocationBean.getInstance().isState()) {
                    mTvStatus.setText("设备已连接");
                } else {
                    mTvStatus.setText("设备未连接");

                }
                if (!TextUtils.isEmpty(LocationBean.getInstance().getLat() + "")) {
                    mTvLat.setText("纬度：" + LocationBean.getInstance().getLat());
                    mTvLon.setText("经度：" + LocationBean.getInstance().getLon());
                    mTvHeight.setText("高度：" + LocationBean.getInstance().getHight());
                    setLocationStatus(LocationBean.getInstance().getPos());
                    mTvSatelliteTotal.setText(mPoints.size() + "   卫星总数：" + LocationBean.getInstance().getSatelliteTotal());
                    mTvSatellite.setText("水平:" + DistanceUtils.decimalFour(LocationBean.getInstance().getHorizontal()) + " 高:" + DistanceUtils.decimalFour(LocationBean.getInstance().getVertrcal()));


                }
            }
        });
    }

    //设置定位状态
    private void setLocationStatus(int status) {
        switch (status) {
            case 0:
                mTvLocationStatus.setText("未解算: 0");
                break;
            case 16:
                mTvLocationStatus.setText("单点定位: 16");
                break;
            case 17:
                mTvLocationStatus.setText("差分定位: 17");
                break;
            case 50:
                mTvLocationStatus.setText("固定解: 50");
                break;
            case 18:
                mTvLocationStatus.setText("SBAS: 18");
                break;
            case 34:
                mTvLocationStatus.setText("浮点解: 34");
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRxTime != null) {
            mRxTime.cancel();
        }
    }

    @Override
    public void onBackPressed() {
        showDialogBack();
    }

    //显示退出对话框
    private void showDialogBack() {

        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("退出");
        normalDialog.setMessage("是否退出");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

    //播放精度不足声音
    private void startMediaPlayer() {
        MediaPlayer.create(this, R.raw.low_precision).start();
    }


    @OnClick({R.id.iv_location, R.id.btn_save, R.id.tv_setting, R.id.tv_measure_state})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_measure_state:
                if (mLine == 3) {
                    showAgainDialog();
                } else {
                    MyToast.show("折线模式才能重测");
                }
                break;
            case R.id.iv_location:
                if (mIsFixed == 1) {
                    int pos = LocationBean.getInstance().getPos();//获取定位状态
                    if (pos == 50) {
                        location();
                    } else {
                        startMediaPlayer();
                        MyToast.show("固定解已锁定");
                    }
                } else {
                    location();
                }

                break;
            case R.id.btn_save:
                if (TextUtils.isEmpty(mCustomBean.getDistance())) {
                    MyToast.show("距离不能为空");
                } else if (TextUtils.isEmpty(mCustomBean.getStartPoint())) {
                    MyToast.show("请测量起始点");
                } else if (TextUtils.isEmpty(mCustomBean.getEndPoint())) {
                    MyToast.show("请测量结束点");
                } else {
                    showSaveDialog();
                }
                break;
            case R.id.tv_setting:
                startActivity(new Intent(this, MeasureSettingActivity.class));
                break;
        }
    }

    //保存
    private void showSaveDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("测量完成");
        normalDialog.setMessage("是否保存");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mCustomBean.setState(1);//将状态设为待上传
                        Intent intent = new Intent();
                        intent.putExtra("data", mCustomBean);
                        setResult(2, intent);
                        finish();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }

    //获取有效的当前经纬度
    private LonLatBean getValidLatLon() {
        MyLogger.i(mPoints.size() + "点数");
        LonLatBean offsetBean = new LonLatBean();
        offsetBean.setLat(LocationBean.getInstance().getLat());
        offsetBean.setLon(LocationBean.getInstance().getLon());
        offsetBean.setHorizontal(LocationBean.getInstance().getHorizontal());
        int pos = LocationBean.getInstance().getPos();//获取定位状态
        if ((pos == 17) || (pos == 50) || (pos == 34) && (offsetBean.getHorizontal() <= mHorizontal)) {

            if (mPoints != null && mPoints.size() > 0) {
                for (int i = 0; i < mPoints.size(); i++) {
                    double distance = DistanceUtils.decimalFour(DistanceUtils.getDistance(offsetBean.getLon(), offsetBean.getLat(), mPoints.get(i).getLon(), mPoints.get(i).getLat()));
                    MyLogger.i("距离" + distance);
                    if (distance > mHorizontal) {
                        mPoints.clear();
                        MyLogger.i("" + mPoints.size());
                        return null;
                    }
                }
                mPoints.add(offsetBean);

            } else {
                mPoints.add(offsetBean);
            }

        } else {
            mPoints.clear();
        }

        return offsetBean;
    }

    //定位
    private void location() {
        try {

            int pos = LocationBean.getInstance().getPos();//获取定位状态
            if ((pos == 16) || (pos == 17) || (pos == 50) || (pos == 34)/*|| (pos == 0)*/) {// 5 》4
                if (((Double.valueOf(SharePreferenceUtils.getString(this, Constant.VERTRCAL, "2")) > LocationBean.getInstance().getVertrcal())
                        && (Double.valueOf(SharePreferenceUtils.getString(this, Constant.HORIZONTAL, "0.5")) > LocationBean.getInstance().getHorizontal()))) {
                    switch (mLine) {
                        case 1:
                            line1();//直线
                            break;
                        case 3:
                            line3(); //折线
                            break;
                    }
                } else {
                    MyToast.show("精度不足 请更改精度设置范围");
                    startMediaPlayer();//播放警告声音
                }


            } else {
                MyToast.show("未结算 请将设备移动空旷位置");
                startMediaPlayer();
            }
        } catch (Exception e) {
            MyCrashHandler.newInstance().saveCrashInfoToFile(e);

        }

    }

    //重新测量
    private void againMeasure() {
        mCustomBean = mAgain;
        mRbEnd.setTextColor(ContextCompat.getColor(this, R.color.black_333333));
        mRbStatr.setTextColor(ContextCompat.getColor(this, R.color.black_333333));
        mRbStatr.setClickable(false);
        mRbEnd.setClickable(true);
        mRbStatr.setChecked(true);
        mDistance = 0.0;
        mTvMeasure.setText("测量结果：" + mDistance);
        mTvtelseme.setVisibility(View.INVISIBLE);
        mCount = 0;
        line3Count = 0;
        mMultipoint = "";
        mTeleseme = 0;
        line1Count = 0;


    }

    //重新测量
    private void showAgainDialog() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("重测");
        normalDialog.setMessage("是否重新测量");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        againMeasure();

                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }

    //初始化定位次数
    private void initMeasureNumber() {
        mNumber = SharePreferenceUtils.getInt(this, Constant.LOCATION_NUMBER, 10) - 1;

    }

    private int mCount;//折线计数

    //折线测量
    private void line3Measure() {
        if (line3Count == 0) { //折线第一次测量
            mPoints.clear();  //每次定位清空
            showDialog();   //定位对话框
            mCount = 1;  //p点 计数
            final RxTime rxTime3 = new RxTime();
            rxTime3.interval(1000, new RxTime.RxAction() {
                @Override
                public void action(long number) {
                    getValidLatLon();
                    MyLogger.i(mPoints.size() + "ppp");
                    if (number == 300) {//超时
                        dismissDialog();
                        rxTime3.cancel();
                        MyToast.show("获取数据超时 请检查设备");
                    }
                    if (mPoints.size() == mNumber) { //获取经纬度的次数
                        LonLatBean offset = average();//平均
                        mOffsetBean = offset;
                        mMultipoint += "{" + offset.getLon() + "," + offset.getLat() + "}";//保存连续点经纬度
                        if (mTeleseme == 0) { //代表开始点
                            mRbStatr.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                            mCustomBean.setStartPoint(offset.getLon() + "," + offset.getLat()); //保存开始点经纬度
                        } else {//结束点
                            mRbEnd.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                            mCustomBean.setEndPoint(offset.getLon() + "," + offset.getLat());
                        }
                        mTvtelseme.setVisibility(View.VISIBLE);
                        mTvtelseme.setText("下次点：p1");
                        line3Count = 1;
                        dismissDialog();
                        rxTime3.cancel();
                        MyToast.show("折线测量完成该点位");
                    }
                }
            });

        } else {
            if (line3Count == 1) { // 1  折线的中间点
                mPoints.clear();
                showDialog();
                mCount++;
                final RxTime rxTime4 = new RxTime();
                rxTime4.interval(1000, new RxTime.RxAction() {
                    @Override
                    public void action(long number) {
                        getValidLatLon();
                        if (number == 300) {
                            dismissDialog();
                            rxTime4.cancel();
                            MyToast.show("获取数据超时 请检查设备");
                        }
                        if (mPoints.size() == mNumber) {
                            LonLatBean offset = average();
                            mMultipoint += "{" + offset.getLon() + "," + offset.getLat() + "}";
                            mDistance += DistanceUtils.decimalTwo(DistanceUtils.getDistance(mOffsetBean.getLon(), mOffsetBean.getLat(), offset.getLon(), offset.getLat()));
                            mTvMeasure.setText("测量结果：" + DistanceUtils.decimalTwo(mDistance));
                            mOffsetBean = offset;
                            dismissDialog();
                            mTvtelseme.setText("下次点：p" + mCount);
                            rxTime4.cancel();
                            MyToast.show("折线测量完成 中间点");
                        }
                    }
                });

            }
        }
        if (line3Count == 2) {//2 表示折线测量 完成
            mPoints.clear();
            showDialog();
            final RxTime rxTime4 = new RxTime();
            rxTime4.interval(1000, new RxTime.RxAction() {
                @Override
                public void action(long number) {
                    getValidLatLon();
                    if (number == 300) {
                        dismissDialog();
                        rxTime4.cancel();
                        MyToast.show("获取数据超时 请检查设备");
                    }
                    if (mPoints.size() == mNumber) {
                        LonLatBean offset = average();
                        mMultipoint += "{" + offset.getLon() + "," + offset.getLat() + "}";
                        mDistance += DistanceUtils.decimalTwo(DistanceUtils.getDistance(mOffsetBean.getLon(), mOffsetBean.getLat(), offset.getLon(), offset.getLat()));
                        mDistance = DistanceUtils.decimalTwo(mDistance);//保持7位小数
                        mTvMeasure.setText("测量结果：" + mDistance);
                        if (mTeleseme == 0) {
                            mRbStatr.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                            mCustomBean.setStartPoint(offset.getLon() + "," + offset.getLat());
                        } else {
                            mRbEnd.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                            mCustomBean.setEndPoint(offset.getLon() + "," + offset.getLat());
                        }

                        mCustomBean.setMultipoint(mMultipoint);//保存连续点经纬度
                        mCustomBean.setDistance(mDistance + "");//保存距离
                        dismissDialog();
                        mTvtelseme.setVisibility(View.INVISIBLE);
                        rxTime4.cancel();
                        MyToast.show("折线测量完成");
                        line3Count = 3;
                    }
                }
            });
        }
    }

    //折线
    private void line3() {
        if (line3Count == 3) { //3 折线测量完成
            MyToast.show("测量已完成 无需再测量");
        } else {
            if ((line1Count == 1) || (line1Count == 2)) {  //1 直线开始测量  2直线测量完成
                MyToast.show("直线测量不能更改");
            } else {
                line3Measure();

            }
        }
    }

    //测量直线
    private void line1() {

        if ((line3Count == 2) || (line3Count == 1)) {
            MyToast.show("不能更改为直线测量");
        } else {
            switch (mTeleseme) {
                case 0: //起点
                    mPoints.clear(); //每次测量清空
                    showDialog();
                    final RxTime rxTime = new RxTime();
                    rxTime.interval(1000, new RxTime.RxAction() {
                        @Override
                        public void action(long number) {
                            getValidLatLon();
                            MyLogger.i(mPoints.size() + "");
                            if (number == 300) {
                                dismissDialog();
                                rxTime.cancel();
                                MyToast.show("获取数据超时 请检查设备");
                            }
                            if (mPoints.size() == mNumber) {
                                LonLatBean offset = average();//平均                                    mLatStart = offset.getLat();
                                mLonStart = offset.getLon();       //获取经度
                                mLatStart = offset.getLat();       //获取经纬
                                mCustomBean.setStartPoint(mLonStart + "," + mLatStart);
                                if (line1Count == 1) {// 根据经纬度算出距离
                                    if ((mLonEnd != null) && (mLatEnd != null)) {
                                        mDistance = DistanceUtils.decimalTwo(DistanceUtils.getDistance(mLonStart, mLatStart, mLonEnd, mLatEnd));
                                        mTvMeasure.setText("测量结果：" + mDistance);
                                        mCustomBean.setDistance(mDistance + "");//保存数据
                                    }
                                }
                                mRbStatr.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                                line1Count = 1;
                                dismissDialog();
                                rxTime.cancel();
                                MyToast.show("直线 开始点测量成功");
                            }
                        }
                    });


                    break;
                case 1: //终点
                    mPoints.clear();
                    showDialog();
                    final RxTime rxTime1 = new RxTime();
                    rxTime1.interval(1000, new RxTime.RxAction() {
                        @Override
                        public void action(long number) {
                            getValidLatLon();
                            if (number == 300) {
                                dismissDialog();
                                rxTime1.cancel();
                                MyToast.show("获取数据超时 请检查设备");
                            }
                            if (mPoints.size() == mNumber) {
                                LonLatBean offset = average();
                                mLatEnd = offset.getLat();
                                mLonEnd = offset.getLon();
                                mCustomBean.setEndPoint(mLonEnd + "," + mLatEnd);
                                if (line1Count == 1) {
                                    if ((mLonStart != null) && (mLatStart != null)) {
                                        mDistance = DistanceUtils.decimalTwo(DistanceUtils.getDistance(mLonStart, mLatStart, mLonEnd, mLatEnd));
                                        mTvMeasure.setText("测量结果：" + mDistance);
                                        DistanceUtils.saveDistance(CustomActivity.this, mDistance);
                                        mCustomBean.setDistance(mDistance + "");
                                    }
                                }
                                mRbEnd.setTextColor(ContextCompat.getColor(CustomActivity.this, R.color.colorBlue));
                                line1Count = 1;
                                dismissDialog();
                                rxTime1.cancel();
                                MyToast.show("直线 结束点测量成功");
                            }
                        }
                    });

                    break;

            }
        }

    }

    //显示定位对话框
    private void showDialog() {
        mDialog = new MeasureLoadingDialog();
        mDialog.show(getSupportFragmentManager(), "");
    }

    //平均
    private LonLatBean average() {
        int total = mPoints.size();
        double lat = 0, lon = 0;
        for (LonLatBean offsetBean : mPoints) {
            lat += offsetBean.getLat() * Math.PI / 180;
            lon += offsetBean.getLon() * Math.PI / 180;
        }
        lat /= total;
        lon /= total;
        LonLatBean offsetBean = new LonLatBean();
        offsetBean.setLat(lat * 180 / Math.PI);
        offsetBean.setLon(lon * 180 / Math.PI);
        return offsetBean;
    }

    //关闭对话框
    private void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }


}
